package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.exception.TianLingException;
import com.example.tianling.common.params.CreateRoomDo;
import com.example.tianling.dao.RoomMapper;
import com.example.tianling.dao.RoomUserMapper;
import com.example.tianling.entity.MatchEntity;
import com.example.tianling.entity.RoomEntity;
import com.example.tianling.entity.RoomUserEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class RoomServiceImpl
        extends ServiceImpl<RoomMapper, RoomEntity>
        implements RoomService {

    @Autowired
    private RoomUserMapper roomUserMapper;

    @Override
    public RoomEntity createRoom(Integer userId, String type) {
        RoomEntity room = new RoomEntity();
        room.setRoomName(type);
        room.setType(type);
        room.setRoomState(0);
        room.setRealNumber(0);
        if (type.equals("无领导")) {
            room.setMaxNumber(8);
        }else {
            room.setMaxNumber(2);
        }
        room.setStartTime(new Date());
        room.setIsMatch(1);
        baseMapper.insert(room);

        return room;
    }
    @Override
    public RoomEntity getRoom(Integer roomId) {
        RoomEntity roomEntity = this.lambdaQuery()
                .eq(RoomEntity::getRoomId, roomId)
                .last("LIMIT 1")
                .one();
        return roomEntity;
    }
    @Override
    public void updateRoomHost(Integer roomId, Integer userId) {
        this.lambdaUpdate()
                .eq(RoomEntity::getRoomId, roomId)
                .set(RoomEntity::getUserId, userId)
                .update();
    }
    @Override
    public void updateRealNumber(Integer roomId, Integer realNumber) {
        this.lambdaUpdate()
                .set(RoomEntity::getRealNumber, realNumber)
                .eq(RoomEntity::getRoomId, roomId)
                .update();
    }
    @Override
    public void removeRoom(Integer roomId) {
        QueryWrapper<RoomEntity> roomWrapper = new QueryWrapper<>();
        roomWrapper.eq("room_id", roomId);
        this.remove(roomWrapper);
    }

    @Override
    public void updateRoomState(String roomId, int roomState) {
        this.lambdaUpdate()
                .set(RoomEntity::getRoomState, roomState)
                .eq(RoomEntity::getRoomId, roomId)
                .update();
    }

    /*
     * 创建房间（更新room表） service层
     * 1.将createRoomDo中的房间信息赋给room
     * 2.将user插入到roomUser表中
     */
    @Override
    public RoomEntity insertRoomByCreate(CreateRoomDo createRoomDo, UserEntity user) {
        RoomEntity room = new RoomEntity();
        room.setRoomName(createRoomDo.getRoomName());
        room.setType(createRoomDo.getType());
        room.setRoomState(0);
        room.setRealNumber(0);
        room.setMaxNumber(createRoomDo.getMaxNumber());
        room.setArea(createRoomDo.getArea());
        room.setPreference(createRoomDo.getPreference());
        room.setIsMatch(0);
        room.setStartTime(new Date());

        baseMapper.insert(room);

        return room;
    }

    /*
     * 预约房间
     * 1.更新room表
     * 2.预约成功则将房间room封装到成功响应中
     * 3.已预约抛出异常 (code=409, msg="已预约房间")
     */
    @Override
    public RoomEntity insertReserveRoom(CreateRoomDo createRoomDo, Integer userId) throws ParseException {
        RoomEntity room = this.lambdaQuery()
                .eq(RoomEntity::getUserId, userId)
                .last("LIMIT 1")
                .one();
        // 3.已预约抛出异常 (code=409, msg="已预约房间")
        if (room != null) { throw new TianLingException(409, "已预约房间");}

        room.setUserId(userId);
        room.setRoomName(createRoomDo.getRoomName());
        room.setRoomState(-1);
        room.setType(createRoomDo.getType());
        room.setRealNumber(0);
        room.setMaxNumber(createRoomDo.getMaxNumber());
        room.setArea(createRoomDo.getArea());
        room.setPreference(createRoomDo.getPreference());

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        room.setStartTime(formatter.parse(createRoomDo.getStartTime()));

        baseMapper.insert(room);

        return room;
    }

    @Override
    public void updateIsMatch(Integer roomId, Integer isMatch) {
        this.lambdaUpdate()
                .set(RoomEntity::getIsMatch, isMatch)
                .eq(RoomEntity::getRoomId, roomId)
                .update();
    }

    @Override
    public void updateBookId(Integer roomId, Integer bookId) {
        this.lambdaUpdate()
                .set(RoomEntity::getBookId, bookId)
                .eq(RoomEntity::getRoomId, roomId)
                .update();
    }


}
